package com.housing.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.zmetaport.common.idempotent.annotation.RepeatSubmit;
import com.zmetaport.common.log.annotation.Log;
import com.zmetaport.common.web.core.BaseController;
import com.zmetaport.common.mybatis.core.page.PageQuery;
import com.zmetaport.common.core.domain.R;
import com.zmetaport.common.core.validate.AddGroup;
import com.zmetaport.common.core.validate.EditGroup;
import com.zmetaport.common.log.enums.BusinessType;
import com.zmetaport.common.excel.utils.ExcelUtil;
import com.housing.domain.vo.DictDataVo;
import com.housing.domain.bo.DictDataBo;
import com.housing.service.IDictDataService;
import com.zmetaport.common.mybatis.core.page.TableDataInfo;

/**
 * 字典数据
 *
 * @author chensm
 * @date 2024-12-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/housing/data")
public class DictDataController extends BaseController {

    private final IDictDataService dictDataService;

    /**
     * 查询字典数据列表
     */
    //@SaCheckPermission("housing:data:list")
    @GetMapping("/list")
    public TableDataInfo<DictDataVo> list(DictDataBo bo, PageQuery pageQuery) {
        return dictDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出字典数据列表
     */
    //@SaCheckPermission("housing:data:export")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DictDataBo bo, HttpServletResponse response) {
        List<DictDataVo> list = dictDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "字典数据", DictDataVo.class, response);
    }

    /**
     * 获取字典数据详细信息
     *
     * @param dictCode 主键
     */
    //@SaCheckPermission("housing:data:query")
    @GetMapping("/{dictCode}")
    public R<DictDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long dictCode) {
        return R.ok(dictDataService.queryById(dictCode));
    }

    /**
     * 新增字典数据
     */
    //@SaCheckPermission("housing:data:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DictDataBo bo) {
        return toAjax(dictDataService.insertByBo(bo));
    }

    @PostMapping("/batch")
    public R<Void> batchSave(@Validated(AddGroup.class) @RequestBody List<DictDataBo> bos) {
        return toAjax(dictDataService.insertByBos(bos));
    }

    /**
     * 修改字典数据
     */
    //@SaCheckPermission("housing:data:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DictDataBo bo) {
        return toAjax(dictDataService.updateByBo(bo));
    }

    /**
     * 删除字典数据
     *
     * @param dictCodes 主键串
     */
    //@SaCheckPermission("housing:data:remove")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] dictCodes) {
        return toAjax(dictDataService.deleteWithValidByIds(List.of(dictCodes), true));
    }
}
